package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.base.Result;
import com.atguigu.spzx.model.vo.base.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author yaya
 * @Create 2024/6/24 8:47
 * Description:
 */
@RestController
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;


    /**
     * 分页查询带条件查询用户信息
     * @param pageNum
     * @param pageSize
     * @param sysUserDto
     * @return
     */
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysUser>> findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                                SysUserDto sysUserDto){
        PageInfo<SysUser> pageInfo = sysUserService.findByPage(pageNum,pageSize,sysUserDto);
        return Result.ok(pageInfo);
    }

    /**
     * 保存用户数据
     * @param sysUser
     * @return
     */
    @PostMapping("/savaSysUser")
    public Result savaSysUser(@RequestBody SysUser sysUser){
        sysUserService.savaSysUser(sysUser);
        return Result.ok();
    }

    /**
     * 更新用户数据
     * @param sysUser
     * @return
     */
    @PutMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return Result.ok();
    }

    /**
     * 通过用户ID删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        sysUserService.deleteById(id);
        return Result.ok();
    }

    /**
     * 根据用户id查询所有的角色列表和该用户拥有的角色
     * @return
     */
    @GetMapping(value = "/getAllRoleList/{currentUserId}")
    public Result<Map<String , Object>> getAllRoleList(@PathVariable Long currentUserId) {
        Map<String, Object> resultMap = sysUserService.getAllRoleList(currentUserId);
        return Result.ok(resultMap)  ;
    }

}
